<template>
  <div class="login">
    <el-form v-if="switchCooke==1" ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">智币精灵</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <i slot="prefix" class="el-icon-user-solid" style="margin:0 8px"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="pwd">
        <el-input
          v-model="loginForm.pwd"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <i slot="prefix" class="el-icon-lock" style="margin:0 8px"></i>
        </el-input>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div class="registerBtn" style="float: right;">
          <span @click="registerBtn">立即注册</span>
        </div>
      </el-form-item>
    </el-form>
    <el-form v-if="switchCooke==0" ref="registerForm" :model="registerForm" :rules="registerFormRules"
             class="login-form">
      <h3 class="title">智币精灵账号注册</h3>
      <el-form-item prop="userName">
        <el-input
          v-model="registerForm.userName"
          type="text"
          placeholder="请输入用户名"
        >
          <i slot="prefix" class="el-icon-user-solid" style="margin:0 8px"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="registerForm.password"
          type="password"
          placeholder="请输入密码"
          show-password
        >
          <i slot="prefix" class="el-icon-lock" style="margin:0 8px"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="email">
        <el-input
          v-model="registerForm.email"
          type="text"
          placeholder="请输入您的邮箱"
        >
          <i slot="prefix" class="el-icon-s-promotion" style="margin:0 8px"></i>
        </el-input>
      </el-form-item>

      <el-form-item prop="registrationCode">
        <el-input
          v-model="registerForm.registrationCode"
          type="text"
          placeholder="请输入邀请码"
        >
          <i slot="prefix" class="el-icon-edit" style="margin:0 8px"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="phone">
        <el-input
          v-model.number="registerForm.phone"
          placeholder="电话"
        >
          <i slot="prefix" class="el-icon-phone" style="margin:0 8px"></i>
        </el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleRegister('registerForm')"
        >
          <span>注册</span>
        </el-button>
        <div class="registerBtn" style="float: right;">
          <span @click="changenLoginBtn">前往登录</span>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2023 robots</span>
    </div>
  </div>
</template>

<script>
import JSEncrypt from 'jsencrypt';
import {ajax} from '@/config/user'
import {refsh} from '@/config/refresh'

export default {
  name: "Login",
  data() {
    //手机号格式校验
    var validatorPhone = function (rule, value, callback) {
      if (value) {
        if (!/^1\d{10}$/.test(value)) {
          callback(new Error('请输入正确的手机号'))
        } else {
          callback()
        }
      }
      callback();
    }
//不允许输入中文验证
   var validateNoChinese=function (rule, value, callback) {
      if (value && /[\u4e00-\u9fa5]/g.test(value)) {
        callback(new Error('用户名不能输入中文'));
      } else {
        callback();
      }
    }
    return {
      registerForm: {
        userName: null,
        password: null,
        email: null,
        phone: "",
        registrationCode: null
      },
      switchCooke: 1,
      codeUrl: "",
      loginForm: {
        username: "",
        pwd: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      //公钥
      publicKey: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQGVu9dhNTWz+kw5vJifAPnBoppjo7MbqQRlbb93JZzu1lKNs/qzatPE+WFZXOSLFBwsx3CX+UUS8GVQ7yQIDaPImAvQvuRk6/a+cIzh7gDhNrfF90IAaepAbwq4c9n9Z+bkSb/KPbZ6WPmMunhPyuEJxACa6RFEenAt4mojFOBwIDAQAB',
      //私钥
      privateKey: 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANAZW712E1NbP6TDm8mJ8A+cGimmOjsxupBGVtv3clnO7WUo2z+rNq08T5YVlc5IsUHCzHcJf5RRLwZVDvJAgNo8iYC9C+5GTr9r5wjOHuAOE2t8X3QgBp6kBvCrhz2f1n5uRJv8o9tnpY+Yy6eE/K4QnEAJrpEUR6cC3iaiMU4HAgMBAAECgYAD4fuknofGlGGs0O5R/yR4uUobraHq6aCetIR5kjWSy2bOh/0FqrIlRVM1OUd6rXLxOsTWq7/mCowjROieFVBruV2Lxk2+PV5lQtKdwpv7xE+T3Oc42Kcm/UcrZjOI6R4sOEceMwq5G1kAQPNgxZM8BJTK5jo3B4VQDML70lvU8QJBAPdtSTnKwMPsyUU9soCHr4BHlsRL78MJtn9k6R4srI9KGwzgBVDR2198QZCm/ucCiJnXkZNEaHKUJ7eOOFejccMCQQDXTzp5pDb9mhq+F9assRItdmCiqY1EHeRzvG8bBH+CdlXNuf1OQS29hkImiPTKW/VvOrMFtzCPCezlrxt7csptAkAkiMmQNAUEqRkLFVIfMmnSqj9z2rabePqukurtHrGYMtwNqaJ/TY1z6vgPcOkgmTosZS1Erowxpvi6XNCq+CJbAkEAxGfimKoC+84CzRsj2j5grZYJyvhOBDAcgLMIyyWMR285EES0XBk652M8nrhaTOob9o4eVn1oT2MnddIJiNAK9QJAHLg9OXkX/UMAP1Q0EFfvCGidrjkAK5kqTTlkRGdZdf/nOVMZlzEhGsBn1cK+nDUvPOC5oKkUWglTcYceamUFPw==',
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "请输入您的账号"}
        ],
        pwd: [
          {required: true, trigger: "blur", message: "请输入您的密码"}
        ],
      },
      registerFormRules: {
        userName: [
          {required: true, trigger: "blur", message: "用户名不能为空"},
          {validator:validateNoChinese, message: "用户名不能输入中文"},
        ],
        password: [
          {required: true, trigger: "blur", message: "密码不能为空"}
        ],
        registrationCode: [
          {required: true, trigger: "blur", message: "邀请码不能为空"}
        ],
        email: [
          {required: true, trigger: "blur", message: "邮箱不能为空"},
          {type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        phone: [
          {validator: validatorPhone, message: "请输入正确的手机号"}
        ],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {

    handleRegister(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const pms = {
            userName: this.registerForm.userName,
            password: this.RSAgetPass(this.registerForm.password),
            email: this.registerForm.email,
            phone: this.registerForm.phone,
            registrationCode: this.registerForm.registrationCode
          }
          ajax.registration(pms).then(res => {
            const {code, msg} = res.data
            if (code == '00000') {
              this.$message.success(msg)
              this.loginForm.username=this.registerForm.userName
              this.loginForm.pwd=this.registerForm.password
              this.registerForm= {
                userName: null,
                password: null,
                email: null,
                phone: "",
                registrationCode: null
              },
              this.switchCooke = 1
            }else {
              this.$message.error(msg)
            }
          })
        } else {
          this.$message.warning('请检查是否输入正确')
          return false;
        }
      });
    },
    registerBtn() {
      this.switchCooke = 0
    },
    changenLoginBtn() {
      this.switchCooke = 1
    },
    async handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          const pms = {
            userName: this.loginForm.username,
            pwd: this.RSAgetPass(this.loginForm.pwd)
          }
          ajax.getlogin(pms).then(res => {
            var {data} = res
            if (data.code == '00000') {
              localStorage.setItem('Authorization', 'Bearer ' + data.data.token)
              localStorage.setItem('userType', data.data.userType)
              localStorage.setItem('userName', this.loginForm.username)
              refsh.refresh().then(ref => {
                this.$router.push('/')
              })

            } else {
              this.$message.error(data.msg)
              this.loading = false;
            }
          })
        } else {
          this.loading = false;
          this.$message.warning('请检查您的账号密码是否输入正确')
        }
      });
    },
    //加密方法
    RSAgetPass(pas) {
      const rsa = new JSEncrypt();
      rsa.setPublicKey(this.publicKey);
      console.log('加密：' + rsa.encrypt(pas))
      return rsa.encrypt(pas)
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.registerBtn {
  span {
    color: #888;
    cursor: pointer;
  }

  span :hover {
    color: #1c84c6;
  }
}

//.div3 {
//  width: 300px;
//  height: 300px;
//  position: relative;
//  overflow: hidden;
//  display: flex;
//  justify-content: center;
//  align-items: center;
//
//  img {
//    position: relative;
//    z-index: 10;
//    width: 280px;
//  }
//}
//
//.div3::after {
//  content: '';
//  position: absolute;
//  left: -50%;
//  top: -50%;
//  width: 200%;
//  height: 200%;
//  background-repeat: no-repeat;
//  background-size: 60% 60%, 40% 40%;
//  background-position: 0 0, 100% 0, 100% 100%, 0 100%;
//  background-image:
//    linear-gradient(to left, #27FE03, rgba(255, 255, 255, 0)),
//    linear-gradient(#814FFE, #814FFE),
//    linear-gradient(#814FFE, #814FFE),
//    linear-gradient(#814FFE, #814FFE),
//    linear-gradient(#814FFE, #814FFE);
//  animation: div3Rotate 3s linear infinite;
//}
//
//.div3>div {
//  position: absolute;
//  left: 5px;
//  top: 5px;
//  width: calc(100% - 10px);
//  height: calc(100% - 10px);
//  background: #ffffff;
//  z-index: 2;
//  padding: 20px;
//  box-sizing: border-box;
//}
//
//@keyframes div3Rotate {
//  100% {
//    transform: rotate(360deg);
//  }
//}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url('https://ab.rockycore.cn/app/admin/img/login.e3347162.png');
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  background-attachment: fixed;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
